package Messages;

import java.io.IOException;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;

import server.SQLConnection;
/**
 * this class implements the freezingsubscriber 
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */
public class MessageFinalFreeze {
	
	private ConnectionToClient client ;
	private String username;
	private String id;
	private String status;
/**
 * constractor of this class 
 * @param client
 */
public MessageFinalFreeze(ConnectionToClient client) {
	super();
	this.client = client;	
	}

/**
 * freezing subscriber and updating it in the DB 
 * @param str
 * @throws IOException
 */
@SuppressWarnings("deprecation")
public void FinalFreeze(String[] str) throws IOException{
		
		try 
		{

			SQLConnection sc = new SQLConnection();
			PreparedStatement id = sc.getConnection().prepareStatement("SELECT * FROM freezesubscriber Where idFreezeSubscriber ='"+str[1]+"';");
			PreparedStatement fr = sc.getConnection().prepareStatement("INSERT INTO freezesubscriber (idFreezeSubscriber,status,date) VALUES(?,?,?);");
			PreparedStatement frup = sc.getConnection().prepareStatement("update db1.freezesubscriber set status = ?  where `idFreezeSubscriber` = '"+str[1]+"'");
			PreparedStatement subid = sc.getConnection().prepareStatement("SELECT * FROM subscriber Where ID ='"+str[1]+"';");
			PreparedStatement subfr = sc.getConnection().prepareStatement("update db1.subscriber set status = ?  where `ID` = '"+str[1]+"'");
			
			
			if (str[2].equals("Freeze")){
			ResultSet rs = subid.executeQuery();
			ResultSet rs1 = id.executeQuery();
			if (rs.next())
			{
			
			java.util.Date today = new java.util.Date();
			java.sql.Date timestamp = new java.sql.Date(today.getTime());
			int day=timestamp.getDate();
			timestamp.setDate(day+Integer.parseInt(str[3]));
			
							if (!rs1.next())
							{
								fr.setString(1, str[1]);
								fr.setString(2, str[2]);
								fr.setDate(3,timestamp);
								fr.executeUpdate();
							}else 
								{	frup.setString(1, str[2]);
									frup.executeUpdate();
									frup.close();
								}
							
									
									subfr.setInt(1,2);
									subfr.executeUpdate();
									
								
							
							subfr.close();	
							fr.close();	
								
							client.sendToClient("frezzed");
			}			
			
			
			}
			
			else if (str[2].equals("UnFreeze"))
			{
				
				PreparedStatement de =sc.getConnection().prepareStatement("DELETE FROM freezesubscriber Where idFreezeSubscriber ='"+str[1]+"';");
				
				
				ResultSet rs = subid.executeQuery();
				ResultSet rs1 = id.executeQuery();
				if (rs.next())
				{
				
					subfr.setInt(1,0);
					subfr.executeUpdate();
				
							if (rs1.next())
							{
								de.executeUpdate();
							}
				}
				client.sendToClient("unfrezzed");
			}
				
			else client.sendToClient("fail");				
								

		}
		 catch (SQLException e)
			{	e.printStackTrace();}
		
		}
	
	
}

